package servlet;

import bean.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "SignServlet", value = "/referee/SignServlet")
public class SignServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=utf-8");
        String id=request.getParameter("id");
        String rid=(String) session.getAttribute("userID");
        String signInfo = "";
        JudgeBean judgeBean=new JudgeBean();
        judgeBean.link();
        judgeBean.loadJudge();
        int flag=0;
        for(Judge a:judgeBean.getJudgeArrayList()){
            if(id.equals(a.getId())&&rid.equals(a.getReferee())){
                flag=1;
            }
        }
        if(flag==1){
            signInfo="您已报名该项目，请重新选择！";
            session.setAttribute("signInfo",signInfo);
            response.sendRedirect("view_apply.jsp");
        }else{
            Judge judge=new Judge(rid,id);
            judgeBean.insertJudge(judge);
            signInfo="您已成功成为此项目裁判";
            session.setAttribute("signInfo",signInfo);
            response.sendRedirect("view_apply.jsp");
        }

    }
}